/*
 * Created on Feb 9, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hezhi.framework.dao;

import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

/**
 * @author PAVANAN
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class DAOUtil {
	/**
	 * 
	 * @param conn
	 *            must be non-null
	 * 
	 *            the caller is responsible for committing the transaction
	 * 
	 * @throws SQLException
	 * 
	 */

	private DAOUtil() {
		// this constructor is intentionally private
	}

	static public Connection getDBConnection() {
		Connection conn = null;

		try {
			InputStream is = DAOUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
			Properties property = new Properties();
			property.load(is);
			String driverClassName = property.getProperty("jdbc.driverClassName");
			String url = property.getProperty("jdbc.url");
			String username = property.getProperty("jdbc.username");
			String password = property.getProperty("jdbc.password");

			Class.forName(driverClassName);
			conn = DriverManager.getConnection(url, username, password);
			conn.setAutoCommit(true);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return conn;
	}

	static public Connection getSQLiteDBConnection() {
		Connection conn = null;

		try {
			SQLiteConfig config = new SQLiteConfig();
			// config.setReadOnly(true);
			config.setSharedCache(true);
			config.enableRecursiveTriggers(true);

			SQLiteDataSource ds = new SQLiteDataSource(config);
			
			//String dbFilePaht = DAOUtil.class.getClassLoader().getResource("/").getPath() + "sample.db";
			//String dbFilePaht = System.getProperty("user.dir") + "/configuration/sample.db";
			
			File tmpFolder = new File ("C:/tmp/");
			if( !tmpFolder.exists()){
				tmpFolder.mkdir();
			}
			
			String dbFilePaht =  "C:/tmp/tpis.db";
			
			ds.setUrl("jdbc:sqlite:" + dbFilePaht);
			conn = ds.getConnection();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return conn;
	}

	public static void closeJDBCConnection(final Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				// log.error(conn, ex);
			}
		}
	}

	public static void closeStatement(final Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException ex) {
				// log.error(stmt, ex);
			}
		}
	}

	public static void closeResultSet(final ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException ex) {
				// log.error(rs, ex);
			}
		}
	}

	public static void closeAllResource(ResultSet rs, Statement stmt,
			Connection conn) {
		closeResultSet(rs);
		closeStatement(stmt);
		closeJDBCConnection(conn);
	}

	static public Object lookup(final String name) {

		Context ctx = getInitialContext();

		Object result = null;

		try {
			result = ctx.lookup(name);
		} catch (NamingException ex) {
			ex.printStackTrace();
			// throw new DAORuntimeException(ex);
		}

		return result;
	}

	static public Context getInitialContext() {
		Context ctx = null;

		try {

			ctx = new InitialContext();
		} catch (NamingException ex) {
			ex.printStackTrace();
			// throw new DAORuntimeException(ex);
		}
		return ctx;
	}

	@SuppressWarnings("unchecked")
	static public Object narrow(final Object obj, final Class clazz) {
		return javax.rmi.PortableRemoteObject.narrow(obj, clazz);
	}

	public static void rollbackConnection(final Connection con) {

		if (con != null) {
			try {
				con.rollback();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	static public Connection getPERFDBConnection() {
		Connection conn = null;

		DataSource ds = null;

		try {

			InitialContext jndiContext = new InitialContext();
			System.out.println(" ************  get lookp *********** ");
			ds = (DataSource) jndiContext.lookup("USDEVAOP1DataSource");

			conn = ds.getConnection();
		} catch (Exception ex) {
			ex.printStackTrace();
			// throw new DAORuntimeException(ex);
		}
		return conn;
	}

}
